<?php
class Date {
	private $db; // database object
	public $is_date_valid;
	private $get_date;
	// properties for the date when the accounting is started - day, month, year, unix time 
	public $start_date;
	public $start_day;
	public $start_month;
	public $start_month_title;
	public $start_year;
	public $start_unix_time;
	//properties for today's date - day,month, year, unix time;
	public $today_date;
	public $today_day;
	public $today_month;
	public $today_month_title;
	public $today_year;
	public $today_unix_time;
	/// properties for the current  date - day,month, year, unix time
	// current date is the date we are working with 
	public $curr_date;
	public $curr_day;
	public $curr_month;
	public $curr_month_title;
	public $curr_year;
	public $curr_unix_time;

	/// properties for the next date - day,month , year
	public $next_date;
	public $next_day;
	public $next_month;
	public $next_month_title;
	public $next_year;
	// properties for previous date - day, month, year
	public $prev_date;
	public $prev_day;
	public $prev_month;
	public $prev_month_title;
	public $prev_year;
	// properties for the last accounted date - day, month, year, unix time
	public $last_date;
	public $last_day;
	public $last_month;
	public $last_month_title;
	public $last_year;
	public $last_unix_time;
	

	function __construct($db_object){
		$this->db = $db_object;
			$this->start_date($this->start_date,
							$this->start_day,
							$this->start_month,
							$this->start_month_title,
							$this->start_year,
							$this->start_unix_time);
			$this->today_date($this->today_date,
							$this->today_day,
							$this->today_month,
							$this->today_month_title,
							$this->today_year,
							$this->today_unix_time);
			$this->curr_date($this->curr_date,
							$this->curr_day,
							$this->curr_month,
							$this->curr_month_title,
							$this->curr_year,
							$this->curr_unix_time);
							
			$this->next_date($this->next_date,$this->next_day);
			$this->next_month_year($this->next_month,$this->next_month_title,$this->next_year);
			$this->prev_date($this->prev_date,$this->prev_day);
			$this->prev_month_year($this->prev_month,$this->prev_month_title,$this->prev_year);
			
			$this->last_date($this->last_date,
							$this->last_day,
							$this->last_month,
							$this->last_month_title,
							$this->last_year,
							$this->last_unix_time);
		
	}	
	public function validate_date($date){
		if(strlen($date)==10){
			$date = explode("-",$date);
			$day = (int) $date[2];
			$month = (int) $date[1];
			$year = (int) $date[0];
			if(checkdate($month,$day,$year)){
				return true;
			}
		} 
		return false;
	}
	private function month_translit($month){
		$array = array(array('January','Януари'),
						array('February','Февруари'),
						array('March','Март'),
						array('April','Април'),
						array('May','Май'),
						array('June','Юни'),
						array('July','Юли'),
						array('August','Август'),
						array('September','Септември'),
						array('October','Октомври'),
						array('November','Ноември'),
						array('December','Декември'));
						
		if(is_integer($month)){
			return $array[$month-1][1];
		}
		for($i=0;$i<12;$i++){
			if($month===substr($array[$i][0],0,3)){
				return substr($array[$i][1],0,6);
			} 
			if ($month===$array[$i][0]){
				return $array[$i][1];
			} 
		}
		
	}
	private function date_split($date,&$day,&$month,&$year){
		$date = explode("-",$date);
		$day = $date[2];
		$month = $date[1];
		$year = $date[0];
	}
	private function get_unix_time($date){
		$this->date_split($date,$day,$month,$year);
		return 	mktime(0,0,0,(int)$month,(int) $day,$year);
	}

	private function start_date(&$date,&$day,&$month,&$month_title,&$year,&$unix){
		$query = $this->db->query("SELECT * FROM balances ORDER BY date asc LIMIT 1");
		if($this->db->num_rows($query)==1){
			$res = $this->db->fetch_assoc($query);
			$date = $res['date'];
		} else {
			$date="1970-01-01";
		}
		$this->date_split($date,$day,$month,$year);
		$month_title = $this->month_translit((int)$month);
		$unix = $this->get_unix_time($date);
	}

	private function today_date(&$date,&$day,&$month,&$month_title,&$year,&$unix ){
		$date = date("Y-m-d");
		$this->date_split($date,$day,$month,$year);
		
		$month_title = $this->month_translit((int)$month);
		$unix = $this->get_unix_time($date);
	}
	private function curr_date(&$date,&$day,&$month,&$month_title,&$year,&$unix){
		if(isset($_GET['date'])){
			$date = $_GET['date'];
		} else {
			$date = $this->today_date;
		}
		$this->date_split($date,$day,$month,$year);
		$month_title = $this->month_translit((int)$month);
		$unix = $this->get_unix_time($date);
	}
	private function next_date(&$date,&$day){
		$date = $this->curr_date;
		$this->date_split($date,$day,$month,$year);
		
		$days_in_month = cal_days_in_month(0,(int)$month,(int)$year);

		if((int)$day < $days_in_month){
			if((int)$day <9){
				$day = "0".((int)$day+1);
			} else {
				$day = (int)$day+1;
			}
		} else {
			$day = "01";
			if((int)$month ==1){
				$month="02";
			} else if ((int)$month==12) {
				$month = "01";
				$year +=1;
			} else if((int) $month <10){
				$month = "0".((int)$month+1);
			} else {
				$month = $month+1;
			}
		}
		
		$date = $year."-".$month."-".$day;
	}
	private function next_month_year(&$month,&$month_title,&$year){
		$date = $this->curr_date;
		$this->date_split($date,$day,$month,$year);
		if((int)$month ==1){
			$month="02";
		} else if ((int)$month==12) {
			$month = "01";
			$year +=1;
		} else if((int) $month <10){
			$month = "0".((int)$month+1);
		} else {
			$month = $month+1;
		}
		$month_title = $this->month_translit((int)$month);
	}
	private function prev_date(&$date,&$day){
		$date = $this->curr_date;
		$this->date_split($date,$day,$month,$year);
		if((int)$day > 1){
			if((int)$day <=10){
				$day = "0".((int)$day-1);
			} else {
				$day = (int)$day-1;
			}
		} else {
			if((int)$month ==1){
				$month="12";
				$year -=1;
			} else if((int) $month <=10){
				$month = "0".((int)$month-1);
			} else {
				$month = $month-1;
			}
			$day = cal_days_in_month(0,(int)$month,(int)$year);
		}
		
		$date = $year."-".$month."-".$day;
	}
	private function prev_month_year(&$month,&$month_title,&$year){
		$date = $this->curr_date;
		$this->date_split($date,$day,$month,$year);
		if((int)$month ==1){
			$month="12";
			$year -=1;
		} else if((int) $month <=10){
			$month = "0".((int)$month-1);
		} else {
			$month = $month-1;
		}
		$month_title = $this->month_translit((int)$month);
	}
	private function last_date(&$date,&$day,&$month,&$month_title,&$year,&$unix){
		$query = $this->db->query("SELECT date from balances order by id desc limit 1");
		$res = $this->db->fetch_assoc($query);
		$date = $res['date'];
		$this->date_split($date,$day,$month,$year);
		$month_title = $this->month_translit((int)$month);
		$unix = $this->get_unix_time($date);
	}
}

?>